﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LifeShare1.Media;
using LifeShare1.Models;
namespace LifeShare1.Controllers
{
    public class MusicController : Controller
    {
        //
        // GET: /Music/
        private MediaService myService = new MediaService();
        public ActionResult MusicIndex(int page = 0)
        {
            int pagesize = 10;
            if (page * pagesize > myService.GetAllMusicInfoes().Count())
                page -= 1;
            if (page <= 0)
                page = 1;
            ViewData["page"] = page;
            MusicInfo[] model = myService.GetAllMusicInfoes();
            return View(model);
        }
        public ActionResult MusicInfoDetails(int resourceID)
        {
            MusicInfo music = myService.GetMusicInfo(resourceID);
            ViewData["MyTitle"] = music.title;
            ViewData["MyAuthor"] = music.author;
            ViewData["MyImageURL"] = music.imageURL;
            ViewData["MyPublisher"] = music.publisher;
            ViewData["MyPubDate"] = music.pubdate;
            ViewData["MyRatingAverage"] = music.ratingAverage;
            ViewData["MyNumRaters"] = music.numRaters;
            ViewData["MySummary"] = music.summary;
            ViewData["MyVersion"] = music.version;
            ViewData["MyTracks"] = music.tracks;
            ViewData["MyDiscs"] = music.discs;
            ViewData["MyRadioURL"] = music.radioURL;
            ViewData["MyResourceID"] = music.ResourceID;
            Comment[] comments = myService.GetCommentList(resourceID);
            if (comments.Count() == 0)
            {
                myService.AddComment(-1, resourceID, "");
            }
            comments = myService.GetCommentList(resourceID);
            return View(comments);
        }
        public ActionResult AddMusicComment(int resourceID)
        {
            MusicInfo music = myService.GetMusicInfo(resourceID);
            ViewData["MyTitle"] = music.title;
            ViewData["MyAuthor"] = music.author;
            ViewData["MyImageURL"] = music.imageURL;
            ViewData["MyPublisher"] = music.publisher;
            ViewData["MyPubDate"] = music.pubdate;
            ViewData["MyRatingAverage"] = music.ratingAverage;
            ViewData["MyNumRaters"] = music.numRaters;
            ViewData["MySummary"] = music.summary;
            ViewData["MyVersion"] = music.version;
            ViewData["MyTracks"] = music.tracks;
            ViewData["MyDiscs"] = music.discs;
            ViewData["MyRadioURL"] = music.radioURL;
            Comment[] comments = myService.GetCommentList(resourceID);
            if (comments.Count() == 0)
            {
                myService.AddComment(-1, resourceID, "");
            }
            comments = myService.GetCommentList(resourceID);
            return View(comments);
        }
        [AcceptVerbs(HttpVerbs.Post), ActionName("AddMusicComment")]
        public ActionResult AddCommentClick(int userID, int resourceID, string NewComment)
        {
            if (NewComment == "")
            {
                ModelState.AddModelError("NewComment", "评论不能为空");
            }
            else
            {
                myService.AddComment(userID, resourceID, NewComment);
            }
            return MusicInfoDetails(resourceID);
        }
        [AcceptVerbs(HttpVerbs.Post), ActionName("MusicIndex")]
        public ActionResult AddMusic(string SubjectIDInput)
        {
            string result = myService.AddMusic(SubjectIDInput);
            if (result == "Invalid imdb")
            {
                ModelState.AddModelError("SubjectID", "SubjectID无效");
                return MusicIndex();
            }
            else
            {
                return MusicIndex();
            }

        }
    }
}
